package cn.wyu.dao;

import cn.wyu.pojo.Information;
import cn.wyu.pojo.User;
import cn.wyu.util.jdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ShopCarDaoImpl implements ShopCarDao {

    private JdbcTemplate template = new JdbcTemplate(jdbcUtils.getDataSource());

    @Override
    public List<Information> findAll() {
        //使用JDBC操作数据库...
        //1.定义sql
        String sql = "select * from ec_shopcar";
        List<Information> informations = template.query(sql, new BeanPropertyRowMapper<Information>(Information.class));

        return informations;
    }

    /**
     * 订单展示
     * @return
     */
    @Override
    public List<Information> findAllOrderForm() {
        //使用JDBC操作数据库...
        //1.定义sql
        String sql = "select * from ec_order";
        List<Information> informations = template.query(sql, new BeanPropertyRowMapper<Information>(Information.class));

        return informations;
    }

    //加入购物车
    @Override
    public void add(Information information) {
        //1.定义sql
        String sql = "insert into ec_shopcar values(null,?,?,?,?,?,?,?,?)";
        //2.执行sql
        template.update(sql,information.getTYPE_CODE(), information.getTITLE(),information.getPRICE(),information.getDISCOUNT(),information.getIMAGE(),information.getBUYNUM(),information.getIntegral(), information.getSupplier()
        );
    }

    //提交订单
    @Override
    public void addOrderForm(Information information) {
        //1.定义sql
        String sql = "insert into ec_order values(null,?,?,?,?,?,?,?,?)";
        //2.执行sql
        template.update(sql,information.getTYPE_CODE(), information.getTITLE(),information.getPRICE(),information.getDISCOUNT(),information.getIMAGE(),information.getBUYNUM(),information.getIntegral(), information.getSupplier()
        );
    }

    @Override
    public void delete(int id) {
        //1.定义sql
        String sql = "delete from ec_shopcar where id = ?";
        //2.执行sql
        template.update(sql, id);
    }

    @Override
    public User findUserByUsernameAndPassword(String username, String password) {
        try {
            String sql = "select * from user where username = ? and password = ?";
            User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public Information findById(int id) {
        String sql = "select * from information where id = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<Information>(Information.class), id);
    }

    @Override
    public void update(Information information) {
        String sql = "update information set name = ?,mmedicine1 = ? ,mnumber1 = ? ,mmedicine2 = ? ,mnumber2 = ?,mmedicine3 = ? ,mnumber3 = ?,mmedicine4 = ? ,mnumber4 = ?,amedicine1 = ? ,anumber1 = ? ,amedicine2 = ? ,anumber2 = ?,amedicine3 = ? ,anumber3 = ?,amedicine4 = ? ,anumber4 = ?,nmedicine1 = ? ,nnumber1 = ? ,nmedicine2 = ? ,nnumber2 = ?,nmedicine3 = ? ,nnumber3 = ?,nmedicine4 = ? ,nnumber4 = ? where id = ?";
        template.update(sql, information.getID()

        );
    }

    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        //1.定义模板初始化sql
        String sql = "select count(*) from user where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //2.遍历map
        Set<String> keySet = condition.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<Object>();
        for (String key : keySet) {

            //排除分页条件参数
            if("currentPage".equals(key) || "rows".equals(key)){
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//？条件的值
            }
        }
        System.out.println(sb.toString());
        System.out.println(params);

        return template.queryForObject(sb.toString(),Integer.class,params.toArray());
    }

    @Override
    public List<User> findByPage(int start, int rows, Map<String, String[]> condition) {
        String sql = "select * from user  where 1 = 1 ";

        StringBuilder sb = new StringBuilder(sql);
        //2.遍历map
        Set<String> keySet = condition.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<Object>();
        for (String key : keySet) {

            //排除分页条件参数
            if("currentPage".equals(key) || "rows".equals(key)){
                continue;
            }

            //获取value
            String value = condition.get(key)[0];
            //判断value是否有值
            if(value != null && !"".equals(value)){
                //有值
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//？条件的值
            }
        }

        //添加分页查询
        sb.append(" limit ?,? ");
        //添加分页查询参数值
        params.add(start);
        params.add(rows);
        sql = sb.toString();
        System.out.println(sql);
        System.out.println(params);

        return template.query(sql,new BeanPropertyRowMapper<User>(User.class),params.toArray());
    }
}
